/*******************************************************************************
 * Copyright (c) 2009 Nirav Thaker.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 *******************************************************************************/
package name.nirav.refactoringman.wizards;

import name.nirav.refactoringman.refactorings.StaticCallForwardingRefactoring;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * @author Nirav Thaker
 *
 */
public class StaticCallForwardInputPage extends UserInputWizardPage implements
		IWizardPage {

	private final StaticCallForwardingRefactoring refactoring;
	private Text methodNameText;

	public StaticCallForwardInputPage(String name, StaticCallForwardingRefactoring refactoring) {
		super(name);
		this.refactoring = refactoring;
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(2, false));
		setControl(composite);
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		label.setText("Forwarded method name: ");
		methodNameText = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		methodNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		methodNameText.setText(refactoring.getOriginalMethod());
		methodNameText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validateInput();
			}
		});
		label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		label.setText("Modifier: ");
		Composite radioGroup = new Composite(composite, SWT.NONE);
		radioGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		radioGroup.setLayout(new GridLayout(2, false));
		
		newRadio(radioGroup, "public");
		newRadio(radioGroup, "protected").setSelection(true);
		validateInput();
	}

	private Button newRadio(Composite radioGroup, String text) {
		Button button = new Button(radioGroup, SWT.RADIO);
		button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		button.setText(text);
		return button;
	}

	protected void validateInput() {
		RefactoringStatus stat= refactoring.setNewMethod(methodNameText.getText());
		setPageComplete(stat);
	}

}
